package com.zmn.plat.admin.controller.channel.product.map;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.exception.PlatException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 渠道产品映射表
 *
 * @author linfeng
 *
 * @since 2020/10/27 15:48
 */
@RestController
@RequestMapping("/channel/product/map/")
public class ChannelProductMapModifyController extends ChannelProductMapBaseController  {

    @ResponseBody
    @RequestMapping("addChannelProductMapping")
    @RequiresPermissions("/channel/product/map/product.action")
    public ResponseDTO addChannelProductMapping(@RequestParam(value = "channelId") Integer channelId, @RequestParam(value = "productId", defaultValue = "0") Integer productId,
                                                @RequestParam(value = "relateId") Integer relateId, @RequestParam(value = "channelProductParentId") String channelProductParentId,
                                                @RequestParam(value = "channelProductSubId") String channelProductSubId) {
        try {
            channelProductMapBService.addEditChannelProductMap(channelId, productId, relateId, channelProductParentId, channelProductSubId);
            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "新增渠道产品映射成功！");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("新增渠道产品映射失败:msg:{},e:{}", e.getMessage(), e);
            return ResponseDTO.fail("新增渠道产品映射失败！");
        }
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author linfeng
     * @since 2020/10/28 17:29
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/channel/product/map/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
                                    @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (NumberUtil.isNullOrZero(status)) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            StatusDTO statusDTO = new StatusDTO();
            statusDTO.setIds(arrayIds);
            statusDTO.setStatus(status);
            statusDTO.setUpdateTime(DateUtil.getNow());
            statusDTO.setUpdater(getMcStaffRealName());
            Integer result = channelProductMapService.updateStatusBatch(statusDTO);
            return ResponseDTO.success(result, "操作成功！");
        } catch (Exception ex) {
            logger.error("修改状态异常,内部错误", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }
}
